package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.Admin_he;
import com.tr.rentcar.model.bean.CarBrand;
import com.tr.rentcar.model.bean.Friendlink;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

public class FriendDao implements ResultHandler<Friendlink>{
	private MySqlDb db=MySqlDb.getInstance();
	/**
	 * 查询所有
	 * @return
	 * @throws SQLException
	 */
	public List<Friendlink> findall() throws SQLException{
		String sql="select * from foreground_link";
		return db.executeQuery(sql, this);
	}
	
	/*
	 通过id找到该对象
	 */
	public Friendlink findbyid(Integer id) throws SQLException{
		String sql="select * from foreground_link where friend_id=?";
		return db.queryUninResult(sql, new GlobalParamHandler(id),this);
	}
	/**
	 * 增加一条记录
	 */
	public boolean save(String name,String address) throws SQLException{
		String sql="insert into foreground_link(name,address) values(?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(name,address))>0;
	}
	/**
	 * 修改
	 */
	public boolean updatefriend( String name, String address,Integer id) throws SQLException {
		String sql = "update  foreground_link set name=?,address=? where friend_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler(name,address,id)) > 0;
	}
	/**
	 * 删除根据id一条记录
	 */
	public boolean frienddeletebyid( Integer fid) throws SQLException {
		String sql = "delete from foreground_link where friend_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler( fid)) > 0;
	}
	
	@Override
	public Friendlink doHander(Map<String, Object> row) {
		Friendlink Fl=new Friendlink();
		Fl.setName(row.get("name")+"");
		Fl.setAddress(row.get("address")+"");
		Fl.setFriendId(Integer.parseInt(row.get("friend_id")+""));
		return Fl;
	}

}
